package com.example.blog.controller;

import com.example.blog.Service.BlogService;
import com.example.blog.model.BlogInfo;
import com.example.blog.utils.JWTUtils;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    List<BlogInfo> blogList = new ArrayList<BlogInfo>();
    @Autowired
    private BlogService blogService;
    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        return blogService.getList();
    }
    @RequestMapping("/getBlogDetails")
    public BlogInfo getBlogDetails(Integer blogId, HttpServletRequest request){
        if(blogId == null ){
            throw new NullPointerException("id不能为空");
        } else if (blogId < 0 ) {
            throw new NullPointerException("不存在该id的博客");
        }
        BlogInfo blogInfo = blogService.getBlogDetails(blogId);
        log.info("blogId:{}",blogId);
        //作者ID
        Integer authorId = blogInfo.getUserID();
        String userToken = request.getHeader("user_token");
        Integer userId = JWTUtils.getUserId(userToken);
        if (authorId!=null && authorId==userId){
            blogInfo.setIsLoginUser(true);
        }
        log.info("islogin:{}",blogInfo.getIsLoginUser());
        log.info("blog:{}",blogInfo);
        return blogInfo;
    }
    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody BlogInfo blog, HttpServletRequest request){
        String userToken = request.getHeader("user_token");
        Integer userId = JWTUtils.getUserId(userToken);
        blog.setUserID(userId);
        Integer result = null;
        try{
            result = blogService.addBlog(blog);
            if(result == 1){
                return true;
            }
        }catch (Exception e){
            log.error("发布博客失败",e.getMessage());
        }
        return false;
    }
@RequestMapping("/update")
    public Boolean updateBlog(@RequestBody BlogInfo blog){
        log.info("blog:{}",blog);
    Integer result = null;
    try{
        result = blogService.updateBlog(blog);
        if(result >= 1){
            log.info("is true");
            return true;
        }
    }catch (Exception e){
        log.info("博客更新失败",e.getMessage());

    }
    log.info("is false");
         return false;
}
@RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId){
    Assert.notNull(blogId,"要删除的博客id不能为空");
    BlogInfo blogInfo = new BlogInfo();
    blogInfo.setId(blogId);
    blogInfo.setDeleteFlag(1);
    Integer result = null;
    try{
        result = blogService.updateBlog(blogInfo);
        if(result >= 1){
            return true;
        }
    }catch (Exception e){
        log.info("博客删除失败",e.getMessage());

    }
    return false;

}

}
